##
## Replication (Figures)
## Sentiment IRT
##

#diret�rio de trabalho
setwd("C:\\Users\\mauricio\\Desktop\\sentiment\\")

#carrega pacotes
library(dplyr)
library(ggplot2)

#carrega dados
tab <- read.csv("sentiment_irt.csv")
final <- read.csv("policy_positions.csv")

##################################

##
## Figure 1
##

#change partie's names
tab$partido <- ifelse(tab$partido == "PDS>PP", "PP",
		   ifelse(tab$partido == "PFL>DEM", "DEM",
		   ifelse(tab$partido == "PL>PR", "PL",
		   ifelse(tab$partido == "PMDB", "MDB",
		   ifelse(tab$partido == "PMR>PRB", "PRB", tab$partido)))))

tiff("figure1.tiff", width = 1000, height = 1900, res = 400)
#png("figure1.png", width = 2000, height = 3800, res = 900)
par(mfrow = c(3,2), mai = c(.25, 0.1, 0.5, 0.1))

#Cardoso 1th term
temp <- subset(tab, tab$term == "Cardoso 1th term")
plot(x = range(temp$idealpt),
	y = range(temp$y),
	type = "n",
	cex = .5, cex.main = 1,
	ylim = c(.5, max(temp$y) + .5),
	xlim = c(-1,2),
	yaxs="i",
	axes=FALSE,
	xlab="", ylab="", main = "Cardoso (1st term)")

abline(v = -1:2, col = gray(.95))

for(i in 1:nrow(temp)){
lines(y = c(temp$y[i],temp$y[i]),  ## line for confidence interval
	x = c(temp$lower[i], temp$upper[i]),
	lwd = 1.2, col = grey(.5))

points(y = temp$y[i],
	x = temp$idealpt[i],
	pch = 21,
	bg = ifelse(temp$partido[i] %in% c("PSDB", "PFL>DEM", "PMDB", "PTB"), "black",
		ifelse(temp$partido[i] == "PDS>PP", "grey", "white")))
}

for(i in 1:nrow(temp)){
text(temp$upper[i], temp$y[i],
	temp$partido[i], cex = .7, pos = 4)
}

axis(1, at = seq(-1,2,1), labels = seq(-1,2,1), cex.axis = .7, lwd = .7, mgp = c(0,.5,0))
axis(3, at = seq(-1,2,1), labels = seq(-1,2,1), cex.axis = .7, lwd = .7, mgp = c(0,.5,0))

#Cardoso 2nd term
temp <- subset(tab, tab$term == "Cardoso 2nd term")
plot(x = range(temp$idealpt),
	y = range(temp$y),
	type = "n",
	cex = .5, cex.main = 1,
	ylim = c(.5, max(temp$y) + .5),
	xlim = c(-1,2),
	yaxs="i",
	axes=FALSE,
	xlab="", ylab="", main = "Cardoso (2nd term)")

abline(v = -1:2, col = gray(.95))

for(i in 1:nrow(temp)){
lines(y = c(temp$y[i],temp$y[i]),  ## line for confidence interval
	x = c(temp$lower[i], temp$upper[i]),
	lwd = 1.2, col = grey(.5))

points(y = temp$y[i],
	x = temp$idealpt[i],
	pch = 21,
	bg = ifelse(temp$partido[i] %in% c("PSDB", "PMDB", "PDS>PP"), "black",
		ifelse(temp$partido[i] == "PFL>DEM", "grey", "white")))
}

for(i in 1:nrow(temp)){
text(temp$upper[i], temp$y[i],
	temp$partido[i], cex = .7, pos = 4)
}

axis(1, at = seq(-1,2,1), labels = seq(-1,2,1), cex.axis = .7, lwd = .7, mgp = c(0,.5,0))
axis(3, at = seq(-1,2,1), labels = seq(-1,2,1), cex.axis = .7, lwd = .7, mgp = c(0,.5,0))

#Lula 1th term
temp <- subset(tab, tab$term == "Lula 1th term")
plot(x = range(temp$idealpt),
	y = range(temp$y),
	type = "n",
	cex = .5, cex.main = 1,
	ylim = c(.5, max(temp$y) + .5),
	xlim = c(-1,2),
	yaxs="i",
	axes=FALSE,
	xlab="", ylab="", main = "Lula (1st term)")

abline(v = -1:2, col = gray(.95))

for(i in 1:nrow(temp)){
lines(y = c(temp$y[i],temp$y[i]),  ## line for confidence interval
	x = c(temp$lower[i], temp$upper[i]),
	lwd = 1.2, col = grey(.5))

points(y = temp$y[i],
	x = temp$idealpt[i],
	pch = 21,
	bg = ifelse(temp$partido[i] %in% c("PT", "PL>PR", "PCdoB", "PSB", "PTB"), "black",
		ifelse(temp$partido[i] %in% c("PDT", "PMDB", "PPS", "PV", "PDS>PP"), "grey", "white")))

}

for(i in 1:nrow(temp)){
text(temp$upper[i], temp$y[i],
	temp$partido[i], cex = .7, pos = 4)
}

axis(1, at = seq(-1,2,1), labels = seq(-1,2,1), cex.axis = .7, lwd = .7, mgp = c(0,.5,0))
axis(3, at = seq(-1,2,1), labels = seq(-1,2,1), cex.axis = .7, lwd = .7, mgp = c(0,.5,0))

#Lula 2nd term
temp <- subset(tab, tab$term == "Lula 2nd term")
plot(x = range(temp$idealpt),
	y = range(temp$y),
	type = "n",
	cex = .5, cex.main = 1,
	ylim = c(.5, max(temp$y) + .5),
	xlim = c(-1,2),
	yaxs="i",
	axes=FALSE,
	xlab="", ylab="", main = "Lula (2nd term)")

abline(v = -1:2, col = gray(.95))

for(i in 1:nrow(temp)){
lines(y = c(temp$y[i],temp$y[i]),  ## line for confidence interval
	x = c(temp$lower[i], temp$upper[i]),
	lwd = 1.2, col = grey(.5))

points(y = temp$y[i],
	x = temp$idealpt[i],
	pch = 21,
	bg= ifelse(temp$partido[i] %in% c("PT", "PL>PR", "PCdoB", "PSB", "PMDB", "PDS>PP", "PMR>PRB"), "black",
		ifelse(temp$partido[i] %in% c("PDT", "PTB"), "grey", "white")))
}

for(i in 1:nrow(temp)){
text(temp$upper[i], temp$y[i],
	temp$partido[i], cex = .7, pos = 4)
}

axis(1, at = seq(-1,2,1), labels = seq(-1,2,1), cex.axis = .7, lwd = .7, mgp = c(0,.5,0))
axis(3, at = seq(-1,2,1), labels = seq(-1,2,1), cex.axis = .7, lwd = .7, mgp = c(0,.5,0))

#Dilma 1th term
temp <- subset(tab, tab$term == "Dilma 1th term")
plot(x = range(temp$idealpt),
	y = range(temp$y),
	type = "n",
	cex = .5, cex.main = 1,
	ylim = c(.5, max(temp$y) + .5),
	xlim = c(-1,2),
	yaxs="i",
	axes=FALSE,
	xlab="", ylab="", main = "Rousseff (1st term)")

abline(v = -1:2, col = gray(.95))

for(i in 1:nrow(temp)){
lines(y = c(temp$y[i],temp$y[i]),  ## line for confidence interval
	x = c(temp$lower[i], temp$upper[i]),
	lwd = 1.2, col = grey(.5))

points(y = temp$y[i],
	x = temp$idealpt[i],
	pch = 21,
	bg = ifelse(temp$partido[i] %in% c("PT", "PL>PR", "PCdoB", "PMDB", "PDT", "PDS>PP"), "black",
		ifelse(temp$partido[i] %in% c("PSB", "PMR>PRB"), "grey", "white")))
}

for(i in 1:nrow(temp)){
text(temp$upper[i], temp$y[i],
	temp$partido[i], cex = .7, pos = 4)
}

axis(1, at = seq(-1,2,1), labels = seq(-1,2,1), cex.axis = .7, lwd = .7, mgp = c(0,.5,0))
axis(3, at = seq(-1,2,1), labels = seq(-1,2,1), cex.axis = .7, lwd = .7, mgp = c(0,.5,0))

#Legend
plot(x = range(temp$idealpt),
	y = range(temp$y),
	type = "n",
	cex = .5,
	ylim = c(.5, max(temp$y) + .5),
	xlim = c(-1,2),
	yaxs="i",
	axes=FALSE,
	xlab="", ylab="")

points(x = -1, y = 5, pch = 21, bg = "black")
text(x = -1, y = 5, "Government", pos = 4, cex = .9)
points(x = -1, y = 4, pch = 21, bg = "white")
text(x = -1, y = 4, "Opposition", pos = 4, cex = .9)
points(x = -1, y = 3, pch = 21, bg = "grey")
text(x = -1, y = 3, "Switched status", pos = 4, cex = .9)

dev.off()

##
## Figure 2
##

ggplot(final, aes(y = idealpt, x = wnominate, label = partido)) +
	ylab("Sentiment-IRT") +
	xlab("W-Nominate") +
	geom_point(size = 3, pch = 1) +
	stat_smooth(method = "loess", col = "black") +
	theme_bw()
ggsave("figure2.tiff", width = 3, height = 2.6, dpi = 400)

##
## Figure 3
##

#wide to long
ideology <- final[, c("pres", "partido", "bls_mean", "idealpt")]
ideology$var <- "Brazilian Legislative Survey"
colnames(ideology)[3] <- "point"

temp <- final[, c("pres", "partido", "eseb_mean", "idealpt")]
temp$var <- "Brazilian Electoral Study"
colnames(temp)[3] <- "point"
ideology <- rbind(ideology, temp)

temp <- final[, c("pres", "partido", "expert", "idealpt")]
temp$var <- "Expert Survey"
colnames(temp)[3] <- "point"
ideology <- rbind(ideology, temp)

temp <- final[, c("pres", "partido", "manifesto", "idealpt")]
temp$var <- "Party Manifesto"
colnames(temp)[3] <- "point"
ideology <- rbind(ideology, temp)

#plot (bls, eseb, expert, manifesto)
ggplot(ideology, aes(y = idealpt, x = point)) +
	facet_wrap( ~ var, scales="free", ncol = 2) +
	ylab("Sentiment-IRT") +
	xlab("Ideology") +
	ylim(c(-1.5,2)) +
	geom_point(size = 3, pch = 1) +
	stat_smooth(method = "loess", col = "black") +
	theme_bw()

ggsave("figure3.tiff", width = 4, height = 4, dpi = 400)
